package com.wyg.state;

import java.util.Random;

/**
 * @description:
 * @author: WYG
 * @time: 2021/4/7 9:28
 */

public class HasQuarterState implements State{
    Random random = new Random(System.currentTimeMillis());
    GumballMachine gumballMachine;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
    @Override
    public void insertQuarter() {
        System.out.println("糖果出来前您不能重复投币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("退回顾客的钱");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("转动曲柄");
        int winner = random.nextInt(10);
        if ((winner == 0) && (gumballMachine.getCount() > 1)) {
            gumballMachine.setState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setState(gumballMachine.getSoldState());
        }

    }

    @Override
    public void dispense() {
        System.out.println("没有糖果可以分配了");
    }
}
